//
//  DownCollectionViewCell.m
//  YiQiTingShu
//
//  Created by dllo on 15/11/13.
//  Copyright © 2015年 直到世界的尽头. All rights reserved.
//

#import "DownCollectionViewCell.h"
#import "FAlbumList.h"

@interface DownCollectionViewCell ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)UIProgressView *progressView;
@property(nonatomic, strong)UILabel *hintLabel;
@property(nonatomic, strong)UIView *headerView;
@end

@implementation DownCollectionViewCell

#pragma mark 进度条
-(UIProgressView *)progressView
{
    if (!_progressView) {
        _progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
        _progressView.frame = FitRectMake(50, 50, 300, 20);
    }
    return _progressView;
}

#pragma mark 头视图
-(UIView *)headerView
{
    if (!_headerView) {
        _headerView = [[UIView alloc] initWithFrame:FitRectMake(0, 0, 375, 40)];
        [_headerView addSubview:self.progressView];
        UILabel *label = [[UILabel alloc] initWithFrame:FitRectMake(60, 5, 300, 40)];
        if ([DownManger shareManger].dowmArray.count != 0) {
            label.text = [[DownManger shareManger].dowmArray[0] downloadUrl];
            [label NightWithType:UIViewColorTypeNormal];
            //NSLog(@"%@", [[DownManger shareManger].dowmArray[0] downloadUrl]);
            [DownTools loadActionWithDownUrl:[[DownManger shareManger].dowmArray[0] downloadUrl] WithProgressView:self.progressView];
        }
    }
    return _headerView;
}

#pragma mark tableView
-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.frame];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView NightWithType:UIViewColorTypeNormal];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        [button setTitle:@"下载完成" forState:UIControlStateNormal];
        button.frame = FitRectMake(0, 0, 375, 40);
        _tableView.tableHeaderView = button;
    }
    return _tableView;
}

#pragma mark 提示文字
-(UILabel *)hintLabel
{
    if (!_hintLabel) {
        _hintLabel = [[UILabel alloc] initWithFrame:FitRectMake(50, 50, 275, 40)];
        [_hintLabel NightWithType:UIViewColorTypeNormal];
        _hintLabel.text = @"暂无下载内容";
        [_hintLabel NightWithType:UIViewColorTypeNormal];
    }
    return _hintLabel;
}

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self NightWithType:UIViewColorTypeNormal];
        [self getData];
        [self addSubview:self.tableView];
        self.tableView.tableFooterView = [[UIView alloc] init];
    }
    return self;
}


-(void)getData
{
    if ([DownManger shareManger].dowmArray.count == 0) {
        [self addSubview:_hintLabel];
    }
}

#pragma mark -tableView代理方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([DownManger shareManger].dowmArray.count - 1 == 0) {
        return 0;
    }
    return [DownManger shareManger].dowmArray.count - 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.textLabel.text = [DownManger shareManger].dowmArray[indexPath.row + 1];
    [cell.textLabel NightWithType:UIViewColorTypeClear];
    [cell NightWithType:UIViewColorTypeClear];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return FitConstant_YorHeight(50);
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return self.headerView;
}

@end
